package com.leonelviera.andronotes;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class UsuarioLoginRegistrarse extends  Activity {
	
	private static final int ACTIVITY_LISTAR_NOTAS = 0;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_registrarse);
        
        Button loginButton = (Button) findViewById(R.id.login);
        loginButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        		AndroNotesWSCalls call = new AndroNotesWSCalls();
        		
        		String email    = ((EditText)findViewById(R.id.email)).getText().toString();
        		String password = ((EditText)findViewById(R.id.password)).getText().toString();
        		
        		Integer user_id = call.login(email, password);
        		
        		if (user_id.intValue() != 0) {
	        		Intent i = new Intent(view.getContext(), UsuarioNotas.class);
	        		i.putExtra(NotasBdAdaptador.COL_USR_ID, user_id);
	        		startActivityForResult(i, ACTIVITY_LISTAR_NOTAS);
        		}
        		else {
        			AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).create();
        			alertDialog.setTitle("Error...");
        			alertDialog.setMessage("El usuario o la contraseña no son válidos");
        			alertDialog.setButton("Intentar de nuevo", new DialogInterface.OnClickListener() {
        			   public void onClick(DialogInterface dialog, int which) {
        			      // here you can add functions
        			   }
        			});
        			alertDialog.show();
        		}
        	}

        });
        
        Button registrarseButton = (Button) findViewById(R.id.registrarse);
        registrarseButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        		AndroNotesWSCalls call = new AndroNotesWSCalls();
        		
        		String email    = ((EditText)findViewById(R.id.email)).getText().toString();
        		String password = ((EditText)findViewById(R.id.password)).getText().toString();
        		
        		Integer user_id = call.register(email, password);
        		
        		if (user_id.intValue() != 0) {
	        		Intent i = new Intent(view.getContext(), UsuarioNotas.class);
	        		i.putExtra(NotasBdAdaptador.COL_USR_ID, user_id);
	        		startActivityForResult(i, ACTIVITY_LISTAR_NOTAS);
        		}
        		else {
        			AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).create();
        			alertDialog.setTitle("Error...");
        			alertDialog.setMessage("No se pudo registrar le usuario, por favor intentá más tarde");
        			alertDialog.setButton("Intentar de nuevo", new DialogInterface.OnClickListener() {
        			   public void onClick(DialogInterface dialog, int which) {
        			      // here you can add functions
        			   }
        			});
        			alertDialog.show();
        		}
        	}

        });
    }
    
}
